package org.langcom.io;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.langcom.log.LookLog;

public class TotalUpdate {

	private static final String WORKSPACE = "workspace";
	private static String pathToProject, pathToTemp, pathToTempNew, executablePath;

	/**
	 * the first arg - path to temp location the second arg - path to tempNew the third arg - path to project Location the fourth arg - path to executable file(if any)
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length > 0) {
			pathToProject = args[0];
		} else {
			return;
		}
		if (args.length > 1) {
			pathToTemp = args[1];
		} else {
			return;
		}
		if (args.length > 2) {
			pathToTempNew = args[2];
		} else {
			return;
		}
		if (args.length > 3) {
			executablePath = args[3];
		}
		try {
			File destDir = new File(pathToProject);
			for (File file : destDir.listFiles()) {
				if (file.getPath().equals(pathToTemp) || (file.isDirectory() && file.getName().equals(WORKSPACE))) {
					continue;
				}
				if (file.isDirectory()) {
					FileUtils.deleteDirectory(file);
				} else {
					file.delete();
				}
			}
			FileUtils.copyDirectory(new File(pathToTempNew), destDir, true);
			FileUtils.deleteDirectory(new File(pathToTempNew));
			if (executablePath != null) {
				Runtime.getRuntime().exec(new String[] {
					executablePath
				});
			}
		} catch (Exception e) {
			LookLog.error(TotalUpdate.class.getName(), "***Error by update ", e);
		}
	}
}
